package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 根据条件分页查询护理项目
     *
     * @param name     护理项目的名称
     * @param status   状态
     * @param pageNum  当前页
     * @param pageSize 每页显示记录数
     * @return
     */
    @Override
    public PageResponse selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1.设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //2.调用mapper接口查询数据
        Page<NursingProjectVo> page = nursingProjectMapper.selectByPage(name, status);
        //3.封装返回的结果
        return PageResponse.of(page, NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto
     */
    @Override
    public void insert(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setCreateTime(new Date());
        nursingProject.setUpdateTime(new Date());
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo selectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 根据修改护理项目
     *
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 根据id删除护理项目
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 修改护理项目的状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updeteStatue(Long id, Integer status) {
        /*NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);*/

        //builder进行一个封装对象,避免直接new一个对象,获取NursingProject里面对数据
         NursingProject nursingProject = NursingProject.builder()
                 .id(id)
                 .status(status)
                 .build();
         nursingProjectMapper.update(nursingProject);

    }
}
